﻿CREATE TRIGGER [IOF_SecurityWindowsGroup_Role_Xref_InsertUpdate] 
ON [dbo].[SecurityWindowsGroup_Role_Xref]
INSTEAD OF INSERT, UPDATE
AS
BEGIN
	SET NOCOUNT ON
	--Handle Updates
	UPDATE SRX
	SET SRX.SRX_SecurityRoleID = i.SRX_SecurityRoleID 
		, SRX.SRX_WindowsGroupID = i.SRX_WindowsGroupID 
		, SRX.SRX_HierarchyID = i.SRX_HierarchyID 
		, SRX.SRX_IncludeChildren = i.SRX_IncludeChildren 
		, SRX.SRX_LastModifiedDate = GETUTCDATE()
		, SRX.SRX_LastModifiedBy = SYSTEM_USER
	FROM dbo.SecurityWindowsGroup_Role_Xref SRX
	INNER JOIN inserted i
	 ON i.SRX_SecurityWindowsGroup_Role_Xref_ID = SRX.SRX_SecurityWindowsGroup_Role_Xref_ID
	INNER JOIN deleted d
	 ON d.SRX_SecurityWindowsGroup_Role_Xref_ID = SRX.SRX_SecurityWindowsGroup_Role_Xref_ID

	INSERT INTO dbo.SecurityWindowsGroup_Role_Xref(SRX_WindowsGroupID
		, SRX_SecurityRoleID
		, SRX_IncludeChildren
		, SRX_HierarchyID 
		, SRX_LastModifiedBy 
		, SRX_LastModifiedDate)
	SELECT i.SRX_WindowsGroupID
		, i.SRX_SecurityRoleID
		, i.SRX_IncludeChildren
		, i.SRX_HierarchyID 
		, SYSTEM_USER
		, GETUTCDATE()
	FROM inserted i
	LEFT OUTER JOIN deleted d
	 on i.SRX_SecurityWindowsGroup_Role_Xref_ID  = d.SRX_SecurityWindowsGroup_Role_Xref_ID 
	WHERE d.SRX_SecurityWindowsGroup_Role_Xref_ID  IS NULL


END